package com.ou.csbs.capstone.views.menu;

import gov.nasa.worldwind.render.Material;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.ou.csbs.capstone.models.AttributeModel;
import com.ou.csbs.capstone.models.shapes.Text;
/**
 * The Text Dialog Swing menu. This is shown whenever the Settings option in the toolbar is selected if you are drawing or have selected a Text object.
 * 
 * @author Senior Capstone
 */
public class TextDrawDialog extends JFrame{
	private static final long serialVersionUID = -255253117429844802L;

	private Color currentColor;
	private double currentFontSize;

	private Text selectedText;
	private Double[] fontSizeOptions = {20d, 24d, 28d, 32d, 36d};

	private JPanel centerPanel = new JPanel();
	private JComboBox<Double> fontSize = new JComboBox<Double>(fontSizeOptions);
	private JButton color = new JButton();
	private JPanel southPanel = new JPanel();
	private JButton ok = new JButton("OK");
	private JButton cancel = new JButton("Cancel");
	
	
	public TextDrawDialog(Text selectedText){
		this.selectedText = selectedText;
		if(this.selectedText != null){
			this.currentColor = this.selectedText.getColor();
			this.currentFontSize = this.selectedText.getFontSize();
		}else{
			this.currentColor = AttributeModel.getAttributes().getOutlineMaterial().getDiffuse();
			this.currentFontSize = 24;
		}
		AttributeModel.setOutlineColor(new Material(this.currentColor));
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setLayout(new BorderLayout());
		fontSize.setSelectedItem(this.currentFontSize);
		color.setBackground(this.currentColor);
		ok.setPreferredSize(new Dimension(90,30));
		cancel.setPreferredSize(new Dimension(90,30));
		
		centerPanel.setLayout(new GridLayout(2, 2, 20, 20));
		centerPanel.add(new JLabel("Font Size:"));
		centerPanel.add(this.fontSize);
		centerPanel.add(new JLabel("Font Color:"));
		centerPanel.add(this.color);
		
		southPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 10));
		southPanel.add(ok);
		southPanel.add(cancel);

		add(centerPanel, BorderLayout.CENTER);
		add(southPanel, BorderLayout.SOUTH);
		pack();
		
		addActionListener();
		setVisible(true);
		
	}
	
	private void addActionListener() {
		color.addActionListener(new dialogListener());
		ok.addActionListener(new dialogListener());
		cancel.addActionListener(new dialogListener());
	}
	
	private class dialogListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource() == ok) {
				if(selectedText != null){
					selectedText.setColor(currentColor);
					selectedText.setFontSize((double) fontSize.getSelectedItem());
				}
				AttributeModel.setOutlineColor(new Material(currentColor));
				AttributeModel.setFontSize((double) fontSize.getSelectedItem());
				dispose();
			}
			else if(e.getSource() == cancel) {
				dispose();
			}
			else if(e.getSource() == color) {
				Color newColor = JColorChooser.showDialog(centerPanel, "Choose outline color", currentColor);
				if(newColor != null) {
					currentColor = newColor;
					color.setBackground(currentColor);
				}
			}
		}
		
	}
}
